package utils.particles
{
	import flash.display.DisplayObject;
	
	import utils.Cartoon;
	
	public class Particle extends Cartoon
	{
		public var xVel					:	Number; 		// the x and y velocity of the particle
		public var yVel					:	Number;
		
		public var drag					:	Number; 		// the drag factor between 0 and 1, where 1 is no drag, and 0 is max drag. 
		
		public var fade					:	Number;		// the fade factor, works as a multiplier. 
		
		public var shrink				:	Number;		// another multiplier that changes the size. 
		// If < 1 then the particle graphic shrinks, >1 and the particle grows. 
		
		public var gravity				:	Number;		// the amount of gravity to add to the yVelocity every frame. If < 0 
		// then gravity goes upwards. 
		
		public var directionRotate:	Boolean; 		// if true then the particle points in the direction it's moving
		
		public var disabled	:	Boolean; 				// for disabling the particle so we can reuse it. 
		
		public var spin					: 	Number; //
		
		public function Particle()
		{
			super();
			// and the default settings
			initDefaults(); 
		}
		
		override public function update(t:Object=null):void
		{
			if(!start || pause)return;
			if(disabled) return ; 
			// add the velocity to the particle's position... 
			x += xVel;
			y += yVel;
			
			// apply drag
			xVel*=drag;
			yVel*=drag;
			
			// fade out
			alpha = ((alpha*1000)*fade)/1000;
			
			// scale
			scaleX = scaleY *=shrink;
			
			// gravity
			yVel+=gravity;
			
			// rotation
			rotation += spin;
			
			if(directionRotate) {
				
				updateRotation(); 
			}
		}
		
		public function initDefaults():void
		{
			drag = 1; 
			fade = 1; 
			shrink = 1; 
			gravity = 0; 
			spin = 0;
			directionRotate = false; 
			enable(); 
			
		}
		
		public function setPos(nx:Number, ny:Number):void
		{
			x = nx; 
			y = ny;
		}
		
		public function setXPos(newx:Number):void
		{
			x = newx;
		}
		
		public function setYPos(newy:Number):void
		{
			y = newy;
		}
		
		public function setVel(xvel:Number, yvel:Number):void
		{
			xVel = xvel;
			yVel = yvel;
		}
		
		public function setScale(newscale:Number):void
		{
			scaleX = scaleY = newscale;
		}
		
		public function setScaleX(n:Number):void
		{
			scaleX = n;
		}
		
		public function setScaleY(n:Number):void
		{
			scaleY = n;
		}
		
		public function restart(x:Number, y:Number):void
		{
			initDefaults();  
			disabled = false; 
			setPos(x,y); 
		}
		
		public function updateRotation():void
		{
			rotation += Math.atan2(yVel,xVel)*(180/Math.PI); 
		}
		
		public function disable():void
		{
			disabled = true; 
			visible= false;
		}
		public function enable():void
		{
			disabled = false; 
			visible = true;
		}
	}
}